package com.hengda.museumonline.utils;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.adapter.MuseumAdapter;
import com.hengda.museumonline.entity.ExlistBean;
import com.hengda.museumonline.http.CallServer;
import com.hengda.museumonline.ui.MainActivity;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import static com.yolanda.nohttp.NoHttp.getContext;

/**
 * @author：JFWU on 2016/10/18 08:34
 * @E-mail：jeffy12138@126.com
 */
public class ListItem {
    public RecyclerView recyclerView;
    private MainActivity mainActivity;
    private int id;
    private int pid;
    private ExlistBean exlistBean = new ExlistBean();

    public ListItem(MainActivity mainActivity, int id, int pid) {
        this.mainActivity = mainActivity;
        this.id = id;
        this.pid = pid;
        initView();
    }

    private void initView() {
        recyclerView = (RecyclerView) View.inflate(mainActivity, R.layout.fragment_display, null);
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        getDataFromServer(pid, id);
    }

    private void getDataFromServer(int pid, int id) {
        Request request = NoHttp.createStringRequest(MyApplication.url + "showroom/exlist", RequestMethod.GET);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(mainActivity).versionName);
        request.add("cityId", id);
        request.add("pid", pid);
        request.add("offset", 0);
        request.add("size", 50);
        CallServer.getRequestInstance().add(mainActivity, pid, request, listener);
    }

    OnResponseListener listener = new OnResponseListener() {
        @Override
        public void onStart(int what) {

        }

        @Override
        public void onSucceed(int what, Response response) {
            int responseCode = response.getHeaders().getResponseCode();// 服务器响应码
            Gson gson = new Gson();
            exlistBean = gson.fromJson(response.get().toString(), ExlistBean.class);
            if (exlistBean != null && exlistBean.getData() != null && exlistBean.getData().size() > 0) {
                recyclerView.setAdapter(new MuseumAdapter(getContext(), exlistBean));
            }

        }

        @Override
        public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {

        }

        @Override
        public void onFinish(int what) {

        }
    };
}
